package com.dkm.orproject.utils.jwt.intertfaceceptor;



import com.dkm.orproject.utils.jwt.intertfaceceptor.domain.CustomException;
import com.dkm.orproject.utils.jwt.intertfaceceptor.domain.Result;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @PROJECT_NAME: orproject
 * @DESCRIPTION: 拦截Controller方法默认返回参数，统一处理返回值/响应体
 * @USER: 周佳佳
 * @DATE: 2020/7/24 10:19
 */
@RestControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice{


    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        if (methodParameter.getMember().getName().equals("uiConfiguration")||methodParameter.getMember().getName().equals("error")
                || methodParameter.getMember().getName().equals("securityConfiguration") || methodParameter.getMember().getName().equals("swaggerResources") || methodParameter.getMember().getName().equals("getDocumentation")) {
            return false;
        }
        return true;
    }

    @Override
    public Result beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        //判断object 是 CustomException
        if (o instanceof  CustomException) {
            CustomException customException = (CustomException) o;
            Result result = new Result();
            result.setCode(customException.getCode());
            result.setMsg(customException.getMsg());
            return result;
        }
        return Result.SUCCESS(o);
    }
}
